package com.gjm.order.service.impl;

import com.gjm.order.dao.objectdata.OrderInfo;
import com.gjm.order.dao.mapper.OrderMapper;
import com.gjm.order.dao.objectdata.ProductInfo;
import com.gjm.order.service.OrderService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource(name = "orderMapper")
    private OrderMapper orderMapper;

    @Resource(name = "restTemplate")
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 计数器，表示当前是第几次运行
     */
    private AtomicInteger count = new AtomicInteger(0);

    /**
     * 防止每次获取到的 instances 不一样
     */
    private List<ServiceInstance> instances;

    /**
     *在对象创建完成后自动执行
     */
    @PostConstruct
    public void initInstances() {
        instances = discoveryClient.getInstances("product-service");
    }

    @Override
    public OrderInfo getOrderInfoByIdWithLoadBalance(Integer orderId) {
        OrderInfo orderInfo = orderMapper.getOrderInfoById(orderId);

        String url = "http://product-service/product/getProductInfoById?productId=" + orderInfo.getProductId();

        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);

        orderInfo.setProductInfo(productInfo);

        return orderInfo;
    }

    /**
     * 使用负载均衡
     * @param orderId
     * @return
     */
//    @Override
//    public OrderInfo getOrderInfoByIdWithLoadBalance(Integer orderId) {
//        OrderInfo orderInfo = orderMapper.getOrderInfoById(orderId);
//
////        String url = "http://127.0.0.1:9090/product/getProductInfoById?productId=" + orderInfo.getProductId();
//
//        int index = count.getAndIncrement() % instances.size();
//        log.info("index: {}", index);
//
//        String uri = instances.get(index).getUri().toString(); //获取 product-service IP 地址 + 端口号
//        log.info("uri: {}", uri);
//
//        String url = uri + "/product/getProductInfoById?productId=" + orderInfo.getProductId();
//
//        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//
//        orderInfo.setProductInfo(productInfo);
//
//        return orderInfo;
//    }

    /**
     * 不使用负载均衡
     * @param orderId
     * @return
     */
    @Override
    public OrderInfo getOrderInfoById(Integer orderId) {
        OrderInfo orderInfo = orderMapper.getOrderInfoById(orderId);

//        String url = "http://127.0.0.1:9090/product/getProductInfoById?productId=" + orderInfo.getProductId();

        List<ServiceInstance> instances = discoveryClient.getInstances("product-service");

        String uri = instances.get(0).getUri().toString(); //获取 product-service IP 地址 + 端口号
        log.info("uri: {}", uri);

        String url = uri + "/product/getProductInfoById?productId=" + orderInfo.getProductId();

        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);

        orderInfo.setProductInfo(productInfo);

        return orderInfo;
    }
}
